// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
/* 引入axios */
import axios from 'axios'


Vue.use(ElementUI);
/* 设置代理的域名 */
axios.defaults.baseURL = '/shop';
/* 设置cookie,session跨域配置 */
axios.defaults.withCredentials=true;
/* 设置post请求体*/
axios.defaults.headers.post['Content-Type'] = 'application/json'

//axios的请求拦截器
axios.interceptors.request.use(function (rs) {
    //发送请求之前做的事情
   //获取token
  //const token = localStorage.getItem("token");
  const token =  sessionStorage.getItem("token");
  //const token =  getCookie("token");
  if(token){
    //赋值token到请求体头部
    rs.headers.Authorization = token;
  }
  return rs;

},function (rs) {
  //发送请求错误做的事情
   return Promise.reject(rs);
})

//axios 响应拦截器
axios.interceptors.response.use(function (rs) {
  //响应之前做的事情
  //统一处理token验证失败做的事情
  if(rs.data.code == 300){
    self.$message(rs.data.msg);
    //回到登录页面
     self.$router.push("/login");
  }
  return rs;
},function (rs) {
  //响应错误做的事情
  return Promise.reject(rs);
})

/* 设置全局axios写法 */
Vue.prototype.$http = axios



Vue.config.productionTip = false

/* eslint-disable no-new */
const self = new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
